package cn.touna.jss.modules.third.car300;

import cn.touna.risk.api.enums.SystemErrorCodeEnum;
import cn.touna.jss.common.factory.ResponseFactory;
import cn.touna.jss.common.properties.disconf.Car300Properties;
import cn.touna.jss.common.util.BeanUtil;
import cn.touna.jss.common.util.HttpUtil;
import cn.touna.jss.modules.adapter.ApiMapping;
import cn.touna.jss.modules.third.car300.entity.Car300Entity;
import cn.touna.jss.modules.third.car300.entity.Car300PriceRequestEntity;
import cn.touna.risk.api.domain.Response;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by hujinhui on 2017/7/28.
 */
@Service
public class Car300Service {
    private static final Logger LOGGER = LoggerFactory.getLogger(Car300Service.class);

    @Autowired
    private Car300Properties config;
    private Response<String> result;

    @Resource(name = "httpRestTemplate")
    private RestTemplate restTemplate;



    /**
     * 获取详细估值分析数据
     *
     * @return
     * @throws Exception
     */
    @ApiMapping(value="car300.getCarPrice",desc="车300.获取详细估值分析数据")
    public Response<String> getCarPrice(Car300PriceRequestEntity request){
        String url = config.getGetUsedCarPriceAnalysisUrl();
        if (StringUtils.isBlank(request.getTransferTimes())) {
            request.setTransferTimes("0");
        }

        Map parm = BeanUtil.transBean2Map(request);
        LOGGER.info("获取工作状况参数：" + request.getWorkState());
        parm.put("token", config.getToken());
        //进行格式转换
        parm.put("transfer_times", request.getTransferTimes());
        parm.put("work_state", request.getWorkState());
        parm.put("model_type", request.getModelType());
        parm.put("metaType", "che300");

        try {
//            String requestUrl = HttpUtil.initParams(url,parm,null);
            String data = HttpUtil.get(url,parm);
//            String data = carPriceRestTemplate.getForObject(requestUrl,String.class);
            return ResponseFactory.success(data);
        } catch (Exception e) {
            return ResponseFactory.fail(SystemErrorCodeEnum.JSS_CALL_EXTSYSTEM_ERROR,e.getMessage());
        }
    }

    /**
     * 基于VIN码获取车型
     * @param request vin
     * @return
     * @throws Exception
     */
    @ApiMapping(value="car300.getCarModelByVin",desc="车300.基于VIN码获取车型")
    public Response<String> getCarModelByVin(Car300Entity request) throws Exception {
        String vin = request.getVin();
        if (StringUtils.isEmpty(vin)) {
            return ResponseFactory.fail(SystemErrorCodeEnum.JSS_PARAM_CHECK_FAILED, "参数vin不能为空");
        }
        StringBuffer url = new StringBuffer(config.getIdentifyModelByVinUrl());
        url.append("?VIN=").append(vin).append("&token=").append(config.getToken());
        try {
            String res = restTemplate.getForObject(url.toString(), String.class);
            return ResponseFactory.success(res);
        } catch (Exception e) {
            return ResponseFactory.fail(SystemErrorCodeEnum.JSS_CALL_EXTSYSTEM_ERROR, e.getMessage());
        }
    }

    /**
     * 根据modelId码获取车辆配置
     * @param request vin
     * @return
     * @throws Exception
     */
    @ApiMapping(value="car300.getModelParameters",desc="车300.根据modelId码获取车辆配置")
    public Response<String> getModelParameters(Car300Entity request) throws Exception {
        String modelId = request.getModelId();
        if (StringUtils.isEmpty(modelId)) {
            return ResponseFactory.fail(SystemErrorCodeEnum.JSS_PARAM_CHECK_FAILED, "参数modelId不能为空");
        }
        StringBuffer url = new StringBuffer(config.getModelParametersUrl());
        url.append("?modelId=").append(modelId).append("&token=").append(config.getNewToken());
        try {
            String res = restTemplate.getForObject(url.toString(), String.class);
            return ResponseFactory.success(res);
        } catch (Exception e) {
            return ResponseFactory.fail(SystemErrorCodeEnum.JSS_CALL_EXTSYSTEM_ERROR, e.getMessage());
        }
    }

    /**
     * 获取品牌列表
     *
     * @return
     */
    public String getCarBrandList() {
        String url = build(config.getCarBrandListUrl(), "");
        return restTemplate.getForObject(url, String.class);
    }

    /**
     * 获取车系列表
     *
     * @param brandId
     * @return
     */
    public String getCarSeriesList(String brandId) {
        String url = build(config.getCarSeriesListUrl(), "&brandId=" + brandId);
        return restTemplate.getForObject(url, String.class);
    }

    /**
     * 获取车型列表
     *
     * @param seriesId
     * @return
     */
    public String getCarModelList(String seriesId) {
        String url = build(config.getCarModelListUrl(), "&seriesId=" + seriesId);
        return restTemplate.getForObject(url, String.class);
    }

    private String build(String url, String append) {
        return new StringBuilder().append(url).append("?token=").append(config.getToken()).append(append).toString();
    }
}
